package cn.edu.scnu.lastandroid.ui.weather

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import cn.edu.scnu.lastandroid.logic.Repository
import cn.edu.scnu.lastandroid.logic.model.AdrLocation
import cn.edu.scnu.lastandroid.logic.model.CityLocation

class WeatherViewModel:ViewModel() {
    private val locationLiveData = MutableLiveData<Any>()
    var cityStr=""
    var lngStr=""
    var latStr=""
    var dayStr=""
    val weatherLiveData = locationLiveData.switchMap { location ->
        when(location){
            is CityLocation -> {
                Log.d("My content", "${location.city}")
                Repository.refreshWeather(location.city)
            }
            is AdrLocation->Repository.refreshWeather(location.lng,location.lat)
            else->throw IllegalArgumentException("未知的类型")
        }
    }

    fun refreshWeather(lng:String,lat:String){
        locationLiveData.value= AdrLocation(lng,lat)
    }
    
    fun refreshWeather(city:String){
        locationLiveData.value= CityLocation(city)
    }
}